package com.lzy.web;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lzy.entity.KeyIM;
import com.lzy.entity.dto.TreeNode;
import com.lzy.exception.WrapedException;
import com.lzy.service.KeyIMService;
import com.lzy.util.Page;
import com.lzy.web.form.JsonResponse4Ext;

@Controller
@RequestMapping(value="/keyIM")
public class KeyIMController {
	
	private KeyIMService imService ;
	
	@Autowired
	public void setImService(KeyIMService imService) {
		this.imService = imService;
	}
	
	@RequestMapping(value="/index")
	public String index(){
		return "keyIM/index";
	}
	
	@RequestMapping(value="/treeLoad")
	@ResponseBody
	public List<TreeNode> treeLoad() throws Exception{
		List<TreeNode> listTreeNodes= this.imService.treeItemsQuery();
		return listTreeNodes;
	}
	
	@RequestMapping(value="/folder/addPre")
	public String addPreFolder(ModelMap modelMap) {
		modelMap.addAttribute("action", "add");
		return "keyIM/folderForm";
	}
	
	@RequestMapping(value="/folder/add")
	@ResponseBody
	public JsonResponse4Ext addFolder(@RequestParam Map<String, Object> params,ModelMap modelMap) {
		this.imService.addFolder(params);
		return new JsonResponse4Ext();
	}
	
	@RequestMapping(value="/folder/load/{id}")
	public String loadFolder(@PathVariable int id, ModelMap modelMap) {
		modelMap.addAttribute("action", "update");
		return "keyIM/folderForm";
	}

	@RequestMapping(value="/folder/update")
	@ResponseBody
	public JsonResponse4Ext updateFolder(@RequestParam Map<String, Object> params, ModelMap modelMap) {
		try {
			 this.imService.updateFolder(params);
		} catch (Exception e) {
			return new JsonResponse4Ext(false);
		}
		return new JsonResponse4Ext();
	}
	
	@RequestMapping(value="/folder/del/{id}")
	@ResponseBody
	public JsonResponse4Ext delFolder(@PathVariable int id) {
		try {
			 this.imService.delFolder(id);
		} catch (Exception e) {
			return new JsonResponse4Ext(false);
		}
		return new JsonResponse4Ext();
	}
	
	/**
	 * 查询KeyIM
	 * @param 目录ID
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/query")
	@ResponseBody
	public Page queryKeyIM(@RequestParam Map<String, Object> params) throws Exception{
		Page page = this.imService.queryKeyIm(params);
		return page;
	}
	
	@RequestMapping(value="/new")
	public String newKeyIm(ModelMap modelMap) {
		modelMap.addAttribute("action", "add");
		return "keyIM/keyForm";
	}
	
	@RequestMapping(value="/add")
	@ResponseBody
	public JsonResponse4Ext addKeyIm(@RequestParam Map<String, Object> params) {
		JsonResponse4Ext response = new JsonResponse4Ext();
		try {
			this.imService.addKeyIm(params);
		} catch (WrapedException e) {
			response.setSuccess(false);
			response.setErrorMessages(e.getMessage());
			return response;
		}
		return response;
	}
	
	@RequestMapping(value="/load/{id}")
	public String loadKeyIm(@PathVariable String id,ModelMap modelMap) {
	 	KeyIM keyIM = this.imService.queryKeyImById(id);
		modelMap.addAttribute("action", "edit");
		modelMap.addAttribute("result",keyIM);
		return "keyIM/keyForm";
	}
	
	@RequestMapping(value="/update")
	@ResponseBody
	public JsonResponse4Ext updateKeyIm(@RequestParam Map<String, Object> params) {
		this.imService.updateKeyIm(params);
		return new JsonResponse4Ext();
	}
	
	@RequestMapping(value="/del")
	@ResponseBody
	public JsonResponse4Ext delKeyIm(@RequestParam Map<String, Object> params,ModelMap modelMap) {
		this.imService.delKeyIm(params);
		return new JsonResponse4Ext();
	}
}
